<!-- /*
*   @Description:
*   @Author:lxy
*   @Date:2023-03-23 10:23
*/ -->
<template>
  <div>
    <DialogTemplate
      ref="DialogTemplateRef"
      v-bind="dialogData"
      @closeHanler="closeHanler"
    >
      <div slot="outerMain" style="padding: 0px 20px">
        <el-form :model="fromData" ref="formRef" :rules="rulesRul">
          <el-form-item label="正文" prop="text">
            <el-input
              :disabled="this.fromDisabled"
              v-model="fromData.text"
              type="textarea"
              :rows="7"
              resize="none"
              placeholder="请输入"
            ></el-input>
          </el-form-item>
        </el-form>
      </div>

      <div slot="outerFooter" v-if="!this.fromDisabled">
        <el-button @click="($event) => buttonClickHandler(0)">取消</el-button>
        <el-button type="primary" @click="($event) => buttonClickHandler(1)"
          >保存</el-button
        >
      </div>
      <div slot="outerFooter" v-else>
        <el-button @click="($event) => buttonClickHandler(0)">关闭</el-button>
      </div>
    </DialogTemplate>
  </div>
</template>

<script>
import DialogTemplate from "@/components/DialogTemplate.vue";
import { dressingByScreening } from "@/components/config/index";
import { addNoticeMessage , editNoticeMessage } from "@/api/notice.js";
import { algorithm, getDateFUnction } from "@/components/config/index";
import _ from "lodash";
export default {
  components: {
    DialogTemplate,
  },
  name: "taskInFoDialog",
  data() {
    return {
      dialogData: {
        title: "默认为测试标题",
        outWidth: "40%",
        destroyOnClose: true,
        top: "40px",
      },
      fromData: {
        text: "",
      },
      rulesRul: {
        text: [{ required: true, message: "正文不能为空", trigger: "blur" }],
      },
      fromDisabled: true,
      titleType: "",
    };
  },

  methods: {
    //开启弹框
    handleOpen(val, titleType) {
      this.fileList = _.cloneDeep(val.imgsrc);
      //1.打开弹框，并为弹框赋值标题
      this.$refs.DialogTemplateRef?.openDialogOne();
      this.dialogData.title = "公告信息 -- " + titleType;
      //2.打开弹框后 判断里面的数据是否可以编辑
      this.titleType = titleType;
      if (titleType == "编辑" || titleType == "新增") {
        this.fromDisabled = false;
      } else {
        this.fromDisabled = true;
      }
      //3.为页面赋初始值
      if (val)
        this.fromData = { ...val };
    },
    //弹出框关闭的回调
    closeHanler() {
      this.$refs.formRef.resetFields();
    },
    //保存/取消按钮点击事件
    buttonClickHandler(type) {
      if (type) {
        this.$refs.formRef.validate((vaild) => {
          if(vaild){
            if (this.titleType == "新增") this.addDataFunction();
            else if (this.titleType == "编辑") this.editDataFunction();
          }
        });
      } else {
        //取消操作
        this.$refs.DialogTemplateRef?.closeDialogOne();
      }
    },

    //新增数据
    addDataFunction() {
      let pre = JSON.parse(sessionStorage.getItem("userMessage"));
      this.fromData.gonggaoID = algorithm(); //公告唯一ID
      this.fromData.gonggaoTime = getDateFUnction(); //公告发布时间
      this.fromData.gonggaoPerpore = pre.username; //公告发布人
      this.fromData.gonggaoPerporeID = pre.userId; //公告发布人ID
      this.fromData.gonggaoPhone = pre.cellphoneNumber; //公告发布人手机号
      addNoticeMessage(this.fromData).then((res) => {
        if (res.data.state == "200") {
          this.$refs.DialogTemplateRef?.closeDialogOne();
          this.$notify.success({ title: "提示", message: "添加成功" });
          this.$emit("resData", 1);
        } else this.$notify.error({ title: "提示", message: res.data.message });
      });
    },
    //编辑数据
    editDataFunction() {
      editNoticeMessage(this.fromData).then((res) => {
        if (res.data.state == 400)
          this.$notify.error({ title: "提示", message: res.data.message });
        else if (!res.data.acknowledged)
          this.$notify.error({ title: "提示", message: "编辑失败" });
        else {
          this.$notify.success({ title: "提示", message: "数据编辑成功" });
          this.$refs.DialogTemplateRef?.closeDialogOne();
          this.$emit("resData", "编辑");
        }
      });
    },
  },
};
</script>

<style scoped lang="less"></style>
